package com.cn.bulu.TEST;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.bulu.constant.CurtainConstant;
import com.cn.bulu.dao.CustomerDao;
import com.cn.bulu.dao.SampleDao;
import com.cn.bulu.pojo.Goods;
import com.cn.bulu.pojo.Goods_special;
import com.cn.bulu.pojo.Sample;
import com.cn.bulu.utils.BuLuStringUtils;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.LuKeBeanUtil;
/**
 * 添加购物车编写并进行测试
 * 添加购物车表明添加的商品类型type:  类型0布艺帘 1成品帘 2配件
 * @author xiaGuanghui 2017年5月26日 下午2:29:18
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mybatis.xml"})
public class AddCarTest {
	
	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private SampleDao sampleDao;
	
	public static final String GOODS_LIST = "[{\"type\":0,\"accs\":[{\"whatStructure\":3},{\"whatStructure\":2}],"
			+ "\"goods\":{\"customId\":1001,\"structure\":0,\"style\":1,\"installCase\":1,\"shadingCloth\":1,"
			+ "\"shadeHead\":1,\"locationId\":\"1\",\"sampleId\":1140}}]";
	
	@Test
	public void testAddCar(){
		//返回参数构造
		BuluResult buluResult = new BuluResult();
		//获取customerId
		Integer customerId = 0;
		//处理并分析参数GoodsList
		JSONArray goodsListJsonArray = JSONArray.parseArray(GOODS_LIST);
		//遍历数组
		for(int index=0;index<goodsListJsonArray.size();index++){
			//下标对应的对象并转为JSONObject
			JSONObject jsonObject = JSONObject.parseObject(goodsListJsonArray.getString(index));
			//获取需要添加购物车的数据
			Integer type =jsonObject.getInteger("type");
			String jsonGoods = jsonObject.getString("goods");
			String accs = jsonObject.getString("accs");
			//输出数据查看
			System.err.println(type+"\n"+jsonGoods+"\n"+accs);
			buluResult = addGoodsToCar(type,jsonGoods,accs,customerId);
		}
		
		System.err.println(buluResult.toString());
		
	}
	
	/**
	 * 添加购物车
	 *xiaGuanghui 2017年6月5日 上午11:39:51 
	 *@param type
	 *@param jsonGoods
	 *@param accs
	 *@return
	 */
	private BuluResult addGoodsToCar(Integer type, String jsonGoods, String accs,Integer customerId) {
		//返回参数构造
		BuluResult buluResult = new BuluResult();
		//获取插入数据的对象对象
		Goods goods = LuKeBeanUtil.fromJSONObjectToBean(jsonGoods, Goods.class);
		goods.setIsCar(CurtainConstant.GOODS_ISCAR);
		goods.setCustomerId(customerId);
		//查询需要数据
		Sample sample = sampleDao.selectSampleBySampleId(LuKeBeanUtil.beanToMap(goods));
		//需要添加的商品如果不是配件,直接插入商品,否则需要计算配件的一些价格
		int insertNum = type==2?addAccsGoodsToCar(type,goods,sample):customerDao.insertCar(goods);
		//判断是否需要为商品添加配件,如果不需要直接返回数据
		if(insertNum>0 && (!BuLuStringUtils.isEmpty(accs))){
			buluResult=addAccsForGoods(type,goods,accs,customerId,sample);
		}else if(insertNum>0 && (BuLuStringUtils.isEmpty(accs))){
			buluResult.setStatus(0);
			buluResult.setMessage("成功添加购物车");
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("添加购物车没有成功,请选择商品重新添加");
		}
		return buluResult;
	}


	/**
	 * 添加购物车时whatStructure的值0代表布配件，1代表纱配件，2帘头配件，3布加纱配件
	 * 如果添加到购物车的商品是布艺帘,则可能存在配件,需要保存商品对应的配件
	 *xiaGuanghui 2017年6月5日 下午1:51:21 
	 *@param type
	 *@param goods
	 *@param accs
	 *@param customerId
	 *@return
	 */
	private BuluResult addAccsForGoods(Integer type, Goods goods, String accs, Integer customerId,Sample sample) {
		//返回参数构造
		BuluResult buluResult = new BuluResult();
		JSONArray accsList = JSONArray.parseArray(accs);
		//查找配件的值
		Map<String,Object> accessoriesType = customerDao.selectAccessoriesTypeById(sample.getAccessoriesTypeId());
		//遍历配件数组
		for (int index = 0; index < accsList.size(); index++) {
			JSONObject accsIndex = JSONObject.parseObject(accsList.getString(index));
			Map<String, Object> special = new HashMap<>();
			special.put("whatStructure", accsIndex.getInteger("whatStructure"));
			if (accsIndex.getInteger("sampleId") != null) {
				Goods accsGoods = LuKeBeanUtil.fromJSONObjectToBean(accsIndex, Goods.class);
				accsGoods.setIsCar(CurtainConstant.GOODS_NOCAR);
				accsGoods.setCustomerId(customerId);
				goods.setAccessoriesTypeId((Integer)accessoriesType.get("accessoriesTypeId"));
				goods.setAccessoriesType((String)accessoriesType.get("accessoriesTypeContent"));
				customerDao.insertCar(accsGoods);
				special.put("accessoriesType", (String)accessoriesType.get("accessoriesTypeContent"));
				special.put("accId", accsGoods.getGoodsId());
			} else {
				if (accsIndex.getInteger("whatStructure") == 3) {
					special.put("accessoriesType", "魔术轨");
				}
			}
			special.put("goodsId", goods.getGoodsId());
			customerDao.insertSpecial(LuKeBeanUtil.mapToBean(special, new Goods_special()));
		}
		buluResult.setStatus(0);
		buluResult.setMessage("成功添加购物车");
		return buluResult;
	}

	/**
	 * 添加配件到购物车需要计算一些价格,然后添加到购物车
	 *xiaGuanghui 2017年6月5日 上午11:47:04 
	 *@param type
	 *@param goods
	 *@return
	 */
	private int addAccsGoodsToCar(Integer type, Goods goods,Sample sample) {
		Integer accessoriesTypeId =sample.getAccessoriesTypeId();
		Double price = sample.getPrice();
		if (accessoriesTypeId == 2 || accessoriesTypeId == 6) {// 直轨 弯轨
			Double goodsPrice = price * goods.getNumber() * goods.getSaleNumber();
			if (goods.getAcc().contains("魔术轨")) {// 代表包含魔术轨
														// 则需再加一个商品 魔术轨
				goodsPrice += goods.getSaleNumber() * customerDao.selectMoshugui();
				goods.setAddpeijian(1);
			}
			goods.setGoodsPrice(goodsPrice);
		} else if (accessoriesTypeId == 1 || accessoriesTypeId == 5) {// 罗马杆
																		// 魔术轨
			Double goodsPrice = price * goods.getNumber() * goods.getSaleNumber();
			goods.setGoodsPrice(goodsPrice);
		} else if (accessoriesTypeId == 3 || accessoriesTypeId == 4) {// 璧钩
																		// 挂绳
			Double goodsPrice = price * goods.getNumber();
			goods.setGoodsPrice(goodsPrice);
		}
		int update = customerDao.insertCar(goods);
		return update;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		// 解析需要加入购物车的商品
		JSONArray list = JSONArray.parseArray(GOODS_LIST);
		for (int i = 0; i < list.size(); i++) {
			// JSON字符串{sampleId ,locationId ,otherRoom}
			Object one = list.get(i);
			System.err.println("===0==="+JSON.toJSON(one));
			JSONObject goodAndAcc = JSONObject.parseObject(one.toString());
			System.err.println("===1==="+JSON.toJSON(goodAndAcc));
			String goods22 = goodAndAcc.getString("goods");
			Goods goods  = LuKeBeanUtil.fromJSONObjectToBean(JSONObject.parseObject(goods22),Goods.class);
			System.out.println("/////"+goods.toString());
			System.out.println("//map///"+LuKeBeanUtil.fromBeanToMap(goods));
			System.err.println("===2==="+goods22);
			if (goods22 == null) {
				goods22 = goodAndAcc.toString();
			}
			String accs = goodAndAcc.getString("accs");
			System.err.println("===3==="+accs);
			JSONObject good = JSONObject.parseObject(goods22);
			System.err.println("===4==="+JSON.toJSON(good));
		}
	}

}
